package com.doctor.windflower_doctor.rectoractivity;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.activity.BaseActivity;
import com.doctor.windflower_doctor.entity.Notes;
import com.doctor.windflower_doctor.fragment.BaseFragment;
import com.doctor.windflower_doctor.recordfragment.SleepChartFragment;
import com.doctor.windflower_doctor.recordfragment.SleepFragment;

public class SleepActivity extends BaseActivity implements OnClickListener {
	private TextView textView;
	private ImageButton back_btn, orenge_btn;
	private TextView week_text;
	private int type = 5;
	private List<Notes> notesList;
	private FragmentManager fragmentManager;
	private BaseFragment currentFragment;
	private int currentSelect = 1;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void onStart() {
		if (currentFragment == null) {
			currentSelect = 1;
		}
		onClick(orenge_btn);
		super.onStart();

	}

	@Override
	protected void setListener() {
		back_btn.setOnClickListener(this);
		orenge_btn.setOnClickListener(this);
	}

	@Override
	protected void findViews() {
		fragmentManager = getSupportFragmentManager();
		textView = (TextView) findViewById(R.id.textView);
		textView.setText("睡眠记录");
		back_btn = (ImageButton) findViewById(R.id.back_btn);
		orenge_btn = (ImageButton) findViewById(R.id.orenge_btn);
		orenge_btn.setVisibility(View.VISIBLE);
		orenge_btn.setImageDrawable(this.getResources().getDrawable(R.drawable.chart));
		week_text = (TextView) findViewById(R.id.week_text);
	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.weight_record;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
			break;
		case R.id.orenge_btn:
			if (currentSelect == 0) {
				orenge_btn.setImageDrawable(this.getResources().getDrawable(R.drawable.list_record));
				currentSelect = 1;
				changeFragment(SleepChartFragment.getInstance(new Bundle()));
			} else {
				orenge_btn.setImageDrawable(this.getResources().getDrawable(R.drawable.chart));
				currentSelect = 0;
				changeFragment(SleepFragment.getInstance(new Bundle()));
			}
			break;
		}
	}

	private void changeFragment(BaseFragment fragment) {
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		if (currentFragment != null) {
			fragmentTransaction.hide(currentFragment);
		}

		if (!fragment.isAdded()) {
			fragmentTransaction.add(R.id.information, fragment);
		} else {
			fragmentTransaction.show(fragment);
		}
		currentFragment = fragment;
		fragmentTransaction.commitAllowingStateLoss();
	}
}
